package com.lcxw.hxzbadmin.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.RequestBodyBasic;
import com.lcxw.hxzbapi.controller.giftandcubecontroller.CubeRealization;
import com.lcxw.hxzbapi.service.CubeDrawRecordService;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.checkerframework.checker.index.qual.PolySameLen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.requestbean.ModCubeRequestParams;
import com.lcxw.hxzbapi.entity.pojo.Cube;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.CubeService;

@RestController
@CrossOrigin
@RequestMapping("admin/cube")
public class CubeController {

    private Logger logger = LoggerFactory.getLogger(CubeController.class);

    @Resource
    CubeService cubeService;

    @Resource
    CubeRealization cubeRealization;

    @PostMapping("/getAll")
    public Response addCube(@RequestBody JSONObject requestParams) {
        logger.info("adminUser {} get all cube;", requestParams.getString("adminName"));
        Response response = new Response();

        Map<String, Object> data = new LinkedHashMap<>();
        List<Cube> cubeList;
        try {
            cubeList = cubeService.selectAll();
        } catch (Exception e) {
            return Response.dbFail(logger, "get all cube");
        }
        data.put("cubeList", cubeList);
        response.setData(data);
        response.setDesc("success");
        return response;
    }

    @PostMapping("/mod")
    public Response modCube(@RequestBody ModCubeRequestParams modCubeRequestParams) {
        logger.info("admin {} mod cube list", modCubeRequestParams.getAdminName());

        List<Cube> cubeList = modCubeRequestParams.getCubes();
        if (cubeList.size() != 8) {
            return Response.error(ErrorCode.INVALID_REQ, "魔方游戏的礼物数量必须为8个");
        }

        try {
            for (Cube cube : cubeList) {
                if (null == cubeService.selectByPrimaryKey(cube.getId())) {
                    cubeService.insert(cube);
                } else {
                    cubeService.updateByPrimaryKey(cube);
                }
            }
        } catch (Exception e) {
            return Response.dbFail(logger, "operate cube");
        }

        return Response.common(logger, "mod cube list.", 0, "success", null);

    }

    /**
     * 获取中奖记录接口
     */
    @PostMapping("/draw/record/list")
    public Response getDrawRecordList(@RequestBody RequestBodyBasic requestBodyBasic) {
        int pageNum = requestBodyBasic.getPageNum();
        int pageSize = requestBodyBasic.getPageSize();
        return cubeRealization.getCubeDrawRecords(pageNum, pageSize);
    }

}
